package cc.rengu.igas.bomp.common.dao.impl;

import cc.rengu.igas.bomp.common.dao.InspectTaskInfoMapper;
import cc.rengu.igas.bomp.common.entity.InspectTaskInfo;
import cc.rengu.oltp.service.common.constant.AppConfigConstant;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.utility.bean.PageInfo;
import cc.rengu.oltp.utility.bean.PageResult;
import cc.rengu.oltp.utility.util.DbsUtil;
import cc.rengu.oltp.utility.util.XmlConfigUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;
import cc.rengu.utility.cache.UnifiedCache;
import cc.rengu.utility.log.RgLog;
import cc.rengu.utility.log.RgLogger;

/**
 * Created by 王成 on 2020/3/5
 * Updated by zhangxuran on 2020/03/31
 */
public class InspectTaskInfoMapperImpl implements InspectTaskInfoMapper {
    private RgLogger rglog = RgLog.getLogger(this.getClass().getName());
    private String dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(AppConfigConstant.DATABASE_CONF_NODE, AppConfigConstant.MCMP_POOL_NAME);

    @Override
    public int insertInspectTaskInfo(InspectTaskInfo inspectTaskInfo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "insertInspectTaskInfo");
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, inspectTaskInfo, null);
        if (0 != iReturnCode) {
            rglog.error("登记巡检任务信息失败,instId:<{}>,inspectTaskNo:<{}>,iReturnCode:<{}>", inspectTaskInfo.getInstId(), inspectTaskInfo.getInspectTaskNo(), iReturnCode);
        }
        return iReturnCode;
    }

    @Override
    public int updateInspectTaskInfoById(InspectTaskInfo inspectTaskInfo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "updateInspectTaskInfoById");
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, inspectTaskInfo, null);
        if (0 != iReturnCode) {
            rglog.error("登记巡检任务信息失败,instId:<{}>,inspectTaskNo:<{}>,id:<{}>,iReturnCode:<{}>", inspectTaskInfo.getInstId(), inspectTaskInfo.getInspectTaskNo(), inspectTaskInfo.getId(), iReturnCode);
        }
        return iReturnCode;
    }

    @Override
    public InspectTaskInfo selectInspectTaskInfoById(String instId, String id) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectInspectTaskInfoById");
        InspectTaskInfo inspectTaskInfo = new InspectTaskInfo();
        inspectTaskInfo.setInstId(instId);
        inspectTaskInfo.setId(id);
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, inspectTaskInfo, inspectTaskInfo);
        if (0 != iReturnCode) {
            rglog.error("获取巡检任务信息失败,instId:<{}>,id:<{}>,iReturnCode:<{}>", instId, id, iReturnCode);
            return null;
        }
        return inspectTaskInfo;
    }

    @Override
    public InspectTaskInfo selectInspectTaskInfoByPrimaryKey(String instId, String inspectTaskNo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectInspectTaskInfoByPrimaryKey");
        InspectTaskInfo inspectTaskInfo = new InspectTaskInfo();
        inspectTaskInfo.setInstId(instId);
        inspectTaskInfo.setInspectTaskNo(inspectTaskNo);
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, inspectTaskInfo, inspectTaskInfo);
        if (0 != iReturnCode) {
            rglog.error("获取巡检任务信息失败,instId:<{}>,inspectTaskNo:<{}>,iReturnCode:<{}>", instId, inspectTaskNo, iReturnCode);
            return null;
        }
        return inspectTaskInfo;
    }

    @Override
    public PageResult selectBompInspectTaskInfoByInstId(String instId, String orgCode, String startDate, String endDate, String empNo, PageInfo pageInfo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectBompInspectTaskInfoByInstId");
        InspectTaskInfo inspectTaskInfo = new InspectTaskInfo();
        inspectTaskInfo.setInstId(instId);
        inspectTaskInfo.setRemark1(orgCode);
        inspectTaskInfo.setInspectBeginTime(startDate);
        inspectTaskInfo.setInspectEndTime(endDate);
        inspectTaskInfo.setOprId(empNo);
        PageResult pageResult = dbsUtil.dbsFetchPageListData(sqlStr, inspectTaskInfo, InspectTaskInfo.class, pageInfo);
        if (null == pageResult) {
            rglog.error("获取巡检任务信息失败,instId:<{}>", instId);
            return null;
        }
        return pageResult;
    }
}
